package com.example.administrator.base.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;


import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UploadLocation extends Service {
    public UploadLocation() {
    }
    boolean x=true;
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        new Thread(new Runnable(){
            @Override
            public void run(){
                while (x)
                {
                    String  lat  = String.valueOf(SharedPreferencesUtils.getParam(UploadLocation.this,"latitude","1")) ;
                    String  lon  = String.valueOf(SharedPreferencesUtils.getParam(UploadLocation.this,"longtitude","1"));
                    Log.i("45819","--->"+lat+lon);
                    Upload(lat,lon);
                    try {
                        Thread.sleep(600000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        x=false;
    }

    /*上传坐标*/
    public void Upload(String  v1, String  v2) {
        final OkHttpClient client = new OkHttpClient();
        Log.i("337899", "1");
        String id = String.valueOf(SharedPreferencesUtils.getParam(UploadLocation.this, "adminId", ""));
        RequestBody formBody = new FormBody.Builder()
                .add("action", "ADMINPOSITION")
                .add("latitude", v1)
                .add("longitude",v2)
                .add("adminId", id)
                .build();
        Log.i("337899", "1");
        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/administrator?")
                .post(formBody)
                .build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("337899", "2");
                Response response ;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        final ReLogout mes = GsonTools.changeGsonToBean(string, ReLogout.class);
                        if (mes!=null) {
                            if (mes.getCode() == 1) {
                                Log.i("337899", "位置上传成功");
                            } else {
                                Log.i("337899", "位置上传失败");
                            }
                        }
                    } else {
                        Log.i("337899", "网络爆炸");
                        throw new IOException("Unexpected code " + response);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }
}